package com.fastwire.carriere.hibernate.daoimpl;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.common.constant.HuaweiInventoryConstants;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.FDFRDao;
import com.fastwire.carriere.inventory.entity.FDFr;
import com.fastwire.carriere.inventory.vo.FDFrVo;

// TODO: Auto-generated Javadoc
/**
 * The Class FDFRDaoImpl.
 */
public class FDFRDaoImpl extends AbstractDAOImpl implements FDFRDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.FDFRDao#findAllFDFRs()
	 */
	@Override
	public void invalidateAll() throws DAOException {
		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		criteria = criteria.add(getStatusCriteria());
		getHibernateTemplate().invalidateObjsByCriteria(criteria);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.FDFRDao#getFDFrsbyVo(com.fastwire.carriere.
	 * inventory.vo.FDFrVo)
	 */
	@Override
	public List<String> getFDFrsbyVo(FDFrVo fdfrVo) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		Criterion criterion = getStatusCriteria();

		if (!StringUtil.isEmptyString(fdfrVo.getNativeEmsName())) {
			criterion = Restrictions.and(
					criterion,
					getWildCardCriterion("emsNativeName",
							fdfrVo.getNativeEmsName()));

		}

		criteria.add(criterion);
		criteria.addOrder(Order.asc("emsNativeName"));

		criteria.setProjection(Projections.distinct(Projections
				.property("emsNativeName")));
		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.FDFRDao#saveOrUpdateFDFR(com.fastwire.carriere
	 * .inventory.entity.FDFr)
	 */
	@Override
	public void saveOrUpdateFDFR(FDFr fDFr) throws DAOException {
		// TODO Auto-generated method stub
		if (fDFr.getOid() == 0)
			super.saveOrUpdate(fDFr);
		else {
			super.merge(fDFr);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.FDFRDao#findFdFrbyName(java.lang.String)
	 */
	@Override
	public List<FDFr> findFdFrbyName(String fdfrName) throws DAOException {
		return findObjListbyName(fdfrName, FDFr.class);
	}

	@Override
	public List<FDFr> findFdFrbyEmsNativeName(String fdfrName)
			throws DAOException {
		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);

		Criterion nameCriterion = Restrictions.eq("emsNativeName", fdfrName);

		criteria.add(nameCriterion);

		criteria = criteria.add(getStatusCriteria());

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.FDFRDao#getAllFDFrEMSNativeName()
	 */
	@Override
	public List getAllFDFrEMSNativeName() throws DAOException {

		return findDistinctAll(FDFr.class, "emsNativeName");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.dao.FDFRDao#findFdFrbyNEName(java.lang.String)
	 */
	@Override
	public List findFdFrbyNEName(String neID) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		criteria = criteria.add(getStatusCriteria());
		criteria.add(Restrictions.like("name", neID, MatchMode.ANYWHERE));

		return getHibernateTemplate().findObjsByCriteria(criteria, true, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.FDFRDao#findFdFrbyNENameAndEmsNativeName(java
	 * .lang.String, java.lang.String)
	 */
	@Override
	public FDFr findFdFrbyNENameAndEmsNativeName(String emsNativeName,
			String neID) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		criteria = criteria.add(getStatusCriteria());
		Criterion emsNativeCriterion = Restrictions.eq("emsNativeName",
				emsNativeName);
		Criterion nameCriterion = Restrictions.like("name", emsNativeName,
				MatchMode.ANYWHERE);

		criteria.add(Restrictions.like("name", neID, MatchMode.ANYWHERE)).add(
				Restrictions.or(emsNativeCriterion, nameCriterion));
		return (FDFr) getHibernateTemplate().findSingleObjByCriteria(criteria,
				false, true);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.FDFRDao#getFDFrNamesbyCriterion(java.lang.String
	 * )
	 */
	@Override
	public List getFDFrNamesbyCriterion(String emsNativeName)
			throws DAOException {

		return getEMSNamesbyCriterion(emsNativeName, FDFr.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.FDFRDao#findFdFrbyTunnelName(java.lang.String)
	 */
	@Override
	public List<FDFr> findFdFrbyTunnelName(String tunnelName)
			throws DAOException {

		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		joinTp(criteria);

		criteria.setFetchMode("zendTp.transParameters", FetchMode.JOIN)
				.createAlias("zendTp.transParameters", "transParameters")
				.setFetchMode("transParameters.parameters", FetchMode.JOIN)
				.createAlias("transParameters.parameters", "parameters")
				.add(Restrictions.and(Restrictions.eq("parameters.name",
						HuaweiInventoryConstants.FDFR_BindingObject),
						Restrictions.like("parameters.value", tunnelName,
								MatchMode.ANYWHERE)));
		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.FDFRDao#findPMPFdFrbyNEName(java.lang.String)
	 */
	@Override
	public List<FDFr> findPMPFdFrbyNEName(String neID) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		Criterion criterion = Restrictions.and(
				Restrictions.like("name", neID, MatchMode.ANYWHERE),
				Restrictions.like("emsNativeName", "VLAN", MatchMode.START));
		criteria = criteria.add(getStatusCriteria());
		criteria.add(criterion);

		return getHibernateTemplate().findObjsByCriteria(criteria, false, true);

	}

	/**
	 * Join tp.
	 * 
	 * @param criteria
	 *            the criteria
	 */
	private void joinTp(DetachedCriteria criteria) {
		criteria.setFetchMode("aendTp", FetchMode.JOIN)
				.createAlias("aendTp", "aendTp")
				.setFetchMode("zendTp", FetchMode.JOIN)
				.createAlias("zendTp", "zendTp");
	}

	@Override
	public List<FDFr> findFdFrbyServiceId(String serviceId) throws DAOException {
		DetachedCriteria criteria = DetachedCriteria.forClass(FDFr.class);
		criteria.add(Restrictions.like("emsNativeName", serviceId,
				MatchMode.ANYWHERE));
		//
		joinTp(criteria);
		criteria.setFetchMode("transParameters", FetchMode.JOIN)
		.createAlias("transParameters", "fdfrTransParameters");
		/*criteria.setFetchMode("zendTp.transParameters", FetchMode.JOIN)
		.createAlias("aendTp.transParameters", "aEndtransParameters");
		criteria.setFetchMode("zendTp.transParameters", FetchMode.JOIN)
		.createAlias("zendTp.transParameters", "zEndtransParameters");*/
		criteria.add(getStatusCriteria());
		return getHibernateTemplate().findObjsByCriteria(criteria, false, false);
	}

}
